package com.mrper.ext.uibase;

import android.app.Activity;
import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;

import com.miguo.ui.R;
import com.mrper.util.CommonUtil;
import com.mrper.util.DeviceUtil;

/**
 * Created by admin on 2016/3/14.
 * 对话框Fragment公共类
 */
public class BasicDialogFragment<B extends ViewDataBinding> extends DialogFragment {

    /** 数据绑定对象 **/
    protected B dataBinder;
    /** 上下文对象 **/
    protected Activity activity;
    /** 窗口对象 **/
    protected Window window;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        dataBinder = DataBindingUtil.inflate(LayoutInflater.from(activity),
                CommonUtil.inflateContentView(getClass()),null,false);
        this.initViews();//初始化视图
        Dialog dialog = new Dialog(activity);
        window = dialog.getWindow();//获取dialog视窗
        setDialogProperties(dialog,window);//设置对话框属性
        dialog.setContentView(dataBinder.getRoot());
        return dialog;
    }

    /**  初始化视图View   **/
    protected void initViews(){}

    /**
     * 设置对话款属性
     * @param dialog 对话框对象
     * @param window window窗口对象
     */
    protected void setDialogProperties(Dialog dialog,Window window){
        window.requestFeature(Window.FEATURE_NO_TITLE);//设置无标题
        window.setBackgroundDrawable(getWindowBackgroundDrawable());
        setDialogLayoutParams(window);
    }

    private void setDialogLayoutParams(Window window) {
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.windowAnimations = getDialogAnimationId();
        layoutParams.gravity = getDialogGravity();
        layoutParams.width = getDialogWidth();
        layoutParams.height = getDialogHeight();
        window.setAttributes(layoutParams);
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = DeviceUtil.getScreenInfo(activity);
        Window window = getDialog().getWindow();
        window.setLayout(dm.widthPixels, window.getAttributes().height);
    }

    /** 获取默认的窗口动画 **/
    protected int getDialogAnimationId(){
        return  R.style.ActionSheetAnimationStyle;
    }


    /** 获取窗口的背景色 **/
    protected Drawable getWindowBackgroundDrawable(){
        return new ColorDrawable(Color.TRANSPARENT);
    }

    /** 获取对话框宽度 **/
    protected int getDialogWidth(){
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    /** 获取对话框的高度  **/
    protected int getDialogHeight(){
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    /** 获取对话框出现的位置 **/
    protected int getDialogGravity(){
        return Gravity.CENTER;
    }

}
